layui.define(
  ["laydate", "table", "laypage", "form", "element"],
  function (exports) {
    var element = layui.element,
      proID = layui.data("proID").key,
      proType = jTools.getQuery("proType"),
      cate = jTools.getQuery("cate"),
      projectName = jTools.getQuery("projectName"),
      projectNameNext = jTools.getQuery("projectNameNext");

    renderProduct();
    //渲染展示文件的html页面
    function renderProductHtml(data) {
      $(".layui-breadcrumb").html(
        '<a href="projectManagement.html">' +
          projectName +
          '</a><a href="javascript:history.go(-1)">' +
          projectNameNext +
          "</a>"
      );
      var str = ""; //用来存储html内容
      if (data.cate !== "-1") {
        for (let key in data.folder) {
          str += `<li class="nextLevelFile" data-id=${data.folder[key]} data-name="${key}"><a title="${key}"><img src="../../layuiadmin/images/filesIcon.png"/><p class="changename">${key}</p></a></li>`;
        }
        $(".projectManagementList #projectManagementListCon").html(str);
        $(".nextLevelFile").on("click", function () {
          var num = $(this).attr("data-id");
          if (num == "-1") {
            location.href =
              "quotationPage.html?proID=" +
              data.proID +
              "&projectName=" +
              projectName +
              "&projectNameNext=" +
              projectNameNext;
          } else {
            var projectNameNextt = $(this).attr("data-name");
            location.href =
              "projectManagementDetail.html?cate=" +
              data.cate +
              "&proID=" +
              data.proID +
              "&num=" +
              num +
              "&projectName=" +
              projectName +
              "&projectNameNext=" +
              projectNameNext +
              "&projectNameNextt=" +
              projectNameNextt;
          }
        });
      } else {
        if (data.otherFile.length == "0") {
          $(".projectManagementList #projectManagementListCon").html(
            "此文件夹为空"
          );
        } else {
          for (let key in data.otherFile) {
            str +=
              `<li class="nextLevelFile" data-id=${data.otherFile[key].id}><a title="${data.otherFile[key].fileName}">` +
              fileSuffixFun(data.otherFile[key].fileName) +
              `<p class="changename">${data.otherFile[key].fileName}</p></a></li>`;
          }
          $(".projectManagementList #projectManagementListCon").html(str);
          $(".nextLevelFile").on("click", function () {
            var id = $(this).attr("data-id");
            window.location.href =
              base_url + "/projectManage/downloadFile?fileID=" + id;
          });
        }
      }
    }

    function fileSuffixFun(item) {
      if (item.indexOf(".doc") != -1) {
        return '<img src="../../layuiadmin/images/word.png"/>';
      } else if (item.indexOf(".jpg") != -1) {
        return '<img src="../../layuiadmin/images/word.png"/>';
      }
    }

    //同步加载文件的数据
    function renderProduct() {
      $.ajax({
        async: false,
        url: base_url + "/projectManage/queryProCate",
        headers: {
          Accept: "application/json; charset=utf-8",
          Authorization: layui.sessionData("userToken").key,
        },
        method: "post",
        contentType: "application/json",
        data: JSON.stringify({ proID, proType, cate }),
        dataType: "json",
        success: function (data) {
          var nextLevelFileData = data.data;
          renderProductHtml(nextLevelFileData);
        },
      });
    }
    $(".backPreviousPage").on("click", function () {
      window.history.back(-1);
    });
    element.init();
    exports("projectManagementNextN", {});
  }
);
